package com.example.diethealthsystem.controller;

import com.example.diethealthsystem.entity.WeightRecord;
import com.example.diethealthsystem.service.WeightRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping
public class WeightRecordController {

    private final WeightRecordService weightRecordService;

    @Autowired
    public WeightRecordController(WeightRecordService weightRecordService) {
        this.weightRecordService = weightRecordService;
    }

    @GetMapping("/api/weightrecords")
    @ResponseBody
    public ResponseEntity<List<WeightRecord>> getAllWeightRecords() {
        List<WeightRecord> weightRecords = weightRecordService.findAll();
        return new ResponseEntity<>(weightRecords, HttpStatus.OK);
    }

    @GetMapping("/api/weightrecords/{id}")
    @ResponseBody
    public ResponseEntity<WeightRecord> getWeightRecordById(@PathVariable Long id) {
        return weightRecordService.findById(id)
                .map(weightRecord -> new ResponseEntity<>(weightRecord, HttpStatus.OK))
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @PostMapping("/api/weightrecords")
    @ResponseBody
    public ResponseEntity<WeightRecord> createWeightRecord(@RequestBody WeightRecord weightRecord) {
        WeightRecord createdWeightRecord = weightRecordService.save(weightRecord);
        return new ResponseEntity<>(createdWeightRecord, HttpStatus.CREATED);
    }

    @PutMapping("/api/weightrecords/{id}")
    @ResponseBody
    public ResponseEntity<WeightRecord> updateWeightRecord(@PathVariable Long id, @RequestBody WeightRecord weightRecord) {
        return weightRecordService.findById(id)
                .map(existingWeightRecord -> {
                    existingWeightRecord.setUser(weightRecord.getUser());
                    existingWeightRecord.setWeight(weightRecord.getWeight());
                    existingWeightRecord.setRecordDate(weightRecord.getRecordDate());
                    WeightRecord updatedWeightRecord = weightRecordService.save(existingWeightRecord);
                    return new ResponseEntity<>(updatedWeightRecord, HttpStatus.OK);
                })
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @DeleteMapping("/api/weightrecords/{id}")
    @ResponseBody
    public ResponseEntity<Void> deleteWeightRecord(@PathVariable Long id) {
        if (weightRecordService.findById(id).isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        weightRecordService.deleteById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping("/api/weightrecords/user/{userId}")
    @ResponseBody
    public ResponseEntity<List<WeightRecord>> getWeightRecordsByUserId(@PathVariable Long userId) {
        List<WeightRecord> weightRecords = weightRecordService.findByUserId(userId);
        if (weightRecords.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(weightRecords, HttpStatus.OK);
    }

    @GetMapping("/weight-trend")
    public String showWeightTrendPage() {
        return "weight-trend";
    }
} 